<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>使用ajax访问本地磁盘</title>
    <style>
       
    </style>
</head>

<body>
    <input type="file" id="datafile">
    <script>
        function readTextFile(fileName){
            var rawFile = new XMLHttpRequest();
            rawFile.open("GET", fileName, true);
            rawFile.onreadystatechange = function(){
                if(rawFile.readyState === 4){
                    if(rawFile.status === 200 || rawFile.status == 0){
                        var allText = rawFile.responseText;
                        console.log(allText)
                    }
                }
            }
            rawFile.send();
        }
        // readTextFile("http://localhost/readFile/text.txt");读取本地文件.html
        readTextFile("D://Astudy/前端/interview/API/读取本地文件/index.txt");


        // function readInputFile(event) {
        //     let file = event.target.files[0];
    
        //     if (!file) {
        //         return;
        //     }
            
        //     let reader = new FileReader();
            
        //     reader.addEventListener('load', event => {
        //         let content = event.target.result;
        //         alert(content);
        //     });
            
        //     reader.readAsText(file);
        // }
        // document.getElementById('datafile').addEventListener('change', readInputFile, false);

    </script>
</body>

</html>